package com.shipwe.loans.ui.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.shipwe.loans.R
import com.shipwe.loans.databinding.ActivityWebviewBinding
import com.shipwe.loans.pro.ShipweActivity
import com.shipwe.loans.utils.event.AppEventPointType
import com.shipwe.loans.utils.helper.CustomConfigsHelper


/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:17
 * @Annotation：WebView
 */
class ActivityWebView : ShipweActivity() {
    private lateinit var binding: ActivityWebviewBinding

    companion object {
        private const val INTENT_LINK_URL = "linkUrl"
        private const val INTENT_LINK_TITLE = "linkTitle"
        fun start(context: Context, linkTitle: String, linkUrl: String) {
            val intent = Intent(context, ActivityWebView::class.java)
            intent.putExtra(INTENT_LINK_URL, linkUrl)
            intent.putExtra(INTENT_LINK_TITLE, linkTitle)
            context.startActivity(intent)
        }
    }

    override fun getShipweRootView(): View {
        binding = ActivityWebviewBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initShipweView() {
        setShipweTitleBarVisibility(View.VISIBLE)
        setShipweTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setShipweTextTitle(getWebViewTitle())
        setShipweTextTitleGravity(Gravity.CENTER)
        setShipweRightBtnRes(R.drawable.customer_black)

        initWebViewSettings()
    }

    override fun initShipweListener() {
        //联系客服
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("${getWebViewTitle()} Page")
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_CUSTOMER,"${getWebViewTitle()} Page")
        }
    }

    override fun initShipweData() {
        binding.webView.loadUrl(getWebViewUrl())
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.webView.setInitialScale(100)
        binding.webView.webChromeClient = mWebChromeClient
        binding.webView.webViewClient = mWebClient
        val webSettings = binding.webView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.webViewProgressBar.progress = newProgress
            if (newProgress == 100) {
                binding.webViewProgressBar.visibility = View.GONE
            } else {
                binding.webViewProgressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?,
        ): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    private fun getWebViewUrl(): String {
        if (intent.extras == null){
            val customData = CustomConfigsHelper.getShipweCustomData()
            if (customData != null) {
                return customData.optString("privacyPolicyLink")
            }
        }
        return intent.extras!!.getString(INTENT_LINK_URL).toString()
    }

    fun getWebViewTitle(): String {
        return intent.extras!!.getString(INTENT_LINK_TITLE).toString()
    }

    override fun onStart() {
        super.onStart()
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_WEBVIEW_LINK_START,"${getWebViewTitle()} Page")
    }

    override fun onDestroy() {
        super.onDestroy()
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_WEBVIEW_LINK_END,"${getWebViewTitle()} Page")
    }
}